home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / platformcfg.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  3.7 KB  |  122 lines

  1. import os
  2. import util
  3. import config
  4. import _winreg
  5. import cPickle
  6. import string
  7. import tempfile
  8. import ctypes
  9.  
  10. _appDataDirectory = None
  11. _baseMoviesDirectory = None
  12.  
  13. def _getRegString(key, subkey):
  14.     def doExpand(val):
  15.         out = ctypes.create_string_buffer(4096)
  16.         indata = ctypes.create_string_buffer(val)
  17.         bytes = ctypes.windll.kernel32.ExpandEnvironmentStringsA(indata,out,4093)
  18.         return out.value
  19.  
  20.     (val, t) = _winreg.QueryValueEx(key, subkey)
  21.     if t == _winreg.REG_SZ:
  22.         return val
  23.     elif t == _winreg.REG_EXPAND_SZ:
  24.         return doExpand(val)
  25.     else:
  26.         raise TypeError, "Got bad type %s for registry subkey %s" % (t, subkey)
  27.  
  28. def _findDirectories():
  29.     global _appDataDirectory
  30.     global _baseMoviesDirectory
  31.  
  32.     keyName = r'Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
  33.     key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, keyName)
  34.  
  35.     try:
  36.         _appDataDirectory = _getRegString(key, 'AppData')
  37.     except:
  38.         # Older versions of Windows didn't have per user Application Data
  39.         keyName2 = r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
  40.         key2 = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, keyName2)
  41.         _appDataDirectory = _getRegString(key2, 'AppData')
  42.     try:
  43.         _baseMoviesDirectory = _getRegString(key, 'My Video')
  44.     except:
  45.         _baseMoviesDirectory = None
  46.     if type(_baseMoviesDirectory) is not str or len(_baseMoviesDirectory) < 1:
  47.         # Apparently some machines have the key present, but blank
  48.         documentsDirectory = _getRegString(key, 'Personal')
  49.         # 'Help' the user
  50.         _baseMoviesDirectory = os.path.join(documentsDirectory, 'My Videos')
  51.  
  52. _findDirectories()
  53.  
  54. def _getMoviesDirectory():
  55.     path = os.path.join(_baseMoviesDirectory, config.get(config.SHORT_APP_NAME))
  56.     try:
  57.         os.makedirs(os.path.join(path, 'Incomplete Downloads'))
  58.     except:
  59.         pass
  60.     return path
  61.  
  62. def _getSupportDirectory():
  63.     path = os.path.join(_appDataDirectory,
  64.                         config.get(config.PUBLISHER),
  65.                         config.get(config.LONG_APP_NAME),
  66.                         'Support')
  67.     try:
  68.         os.makedirs(path)
  69.     except:
  70.         pass
  71.     return path
  72.  
  73. def _getConfigFile():
  74.     return os.path.join(_getSupportDirectory(), "preferences.bin")
  75.  
  76. def load():
  77.     try:
  78.         file = _getConfigFile()
  79.         return cPickle.load(open(file))
  80.     except:
  81.         print "Error loading perferences. Resetting prefs."
  82.         return {}
  83.  
  84. def save(data):
  85.     file = _getConfigFile()
  86.     cPickle.dump(data,open(file,'w'))
  87.  
  88. def get(descriptor):
  89.     if descriptor == config.MOVIES_DIRECTORY:
  90.         return _getMoviesDirectory()
  91.  
  92.     elif descriptor == config.SUPPORT_DIRECTORY:
  93.         return _getSupportDirectory()
  94.     
  95.     elif descriptor == config.DB_PATHNAME:
  96.         path = get(config.SUPPORT_DIRECTORY)
  97.         return os.path.join(path, 'tvdump')
  98.  
  99.     elif descriptor == config.LOG_PATHNAME:
  100.         return os.path.join(tempfile.gettempdir(), 'dtv-log')
  101.  
  102.     elif descriptor == config.DOWNLOADER_LOG_PATHNAME:
  103.         return os.path.join(tempfile.gettempdir(), 'dtv-downloader-log')
  104.  
  105.     elif descriptor == config.RUN_AT_STARTUP:
  106.         # We use the legacy startup registry key, so legacy versions
  107.         # of Windows have a chance
  108.         # http://support.microsoft.com/?kbid=270035
  109.  
  110.         folder = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"Software\Microsoft\Windows\CurrentVersion\Run")
  111.         count = 0
  112.         while True:
  113.             try:
  114.                 (name, val, type) = _winreg.EnumValue(folder,count)
  115.                 count += 1
  116.                 if (name == "Democracy Player"):
  117.                     return True                    
  118.             except:
  119.                 return False
  120.         return False
  121.     return None
  122.